package com.meeting.controller;

import com.meeting.entity.EquipmentType;
import com.meeting.service.EquipmentTypeService;
import com.meeting.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/equipmentType")
public class EquipmentTypeController {

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    @GetMapping("/selectAll")
    public Result getAllEquipmentTypes() {
        log.info("获取所有设备类型");
        try {
            List<EquipmentType> equipmentTypes = equipmentTypeService.getAllEquipmentTypes();
            return Result.success(equipmentTypes);
        } catch (Exception e) {
            log.error("获取所有设备类型失败", e);
            return Result.error("获取设备类型失败");
        }
    }

    @GetMapping("/selectById/{id}")
    public Result getEquipmentTypeById(@PathVariable Integer id) {
        log.info("获取设备类型详情，ID：{}", id);
        try {
            EquipmentType equipmentType = equipmentTypeService.getById(id);
            if (equipmentType == null) {
                return Result.error("设备类型不存在");
            }
            return Result.success(equipmentType);
        } catch (Exception e) {
            log.error("获取设备类型详情失败，ID：{}", id, e);
            return Result.error("获取设备类型详情失败");
        }
    }

    @PostMapping("/save")
    public Result addEquipmentType(@RequestBody EquipmentType equipmentType) {
        log.info("添加设备类型：{}", equipmentType);
        try {
            boolean success = equipmentTypeService.save(equipmentType);
            return success ? Result.success("添加成功") : Result.error("添加失败");
        } catch (Exception e) {
            log.error("添加设备类型失败：{}", equipmentType, e);
            return Result.error("添加设备类型失败");
        }
    }

    @PutMapping("/update")
    public Result updateEquipmentType(@RequestBody EquipmentType equipmentType) {
        log.info("更新设备类型：{}", equipmentType);
        try {
            boolean success = equipmentTypeService.updateById(equipmentType);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新设备类型失败：{}", equipmentType, e);
            return Result.error("更新设备类型失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteEquipmentType(@PathVariable Integer id) {
        log.info("删除设备类型，ID：{}", id);
        try {
            boolean success = equipmentTypeService.removeById(id);
            return success ? Result.success("删除成功") : Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除设备类型失败，ID：{}", id, e);
            return Result.error("删除设备类型失败");
        }
    }
}